<template>
  <div class="list" ref="list">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.currentCity }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item in city" :key="item.id">
            <div class="button" @click="handleCityClick(item.name)">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item,key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>
        <div class="item-list">
          <div
            class="item border-bottom"
            v-for="item in item"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >{{ item.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Bscroll from "better-scroll";
import { mapState } from "vuex";
import { mapMutations } from "vuex"
export default {
  computed: {
    ...mapState({
      currentCity: "city"
    })
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.list);
  },
  props: {
    city: Array,
    cities: Object,
    letter: String
  },
  methods: {
    handleCityClick(city) {
      // this.$store.commit("changeCity", city);另一种写法
      this.changeCity(city)
      this.$router.push("/");
    },
    ...mapMutations(['changeCity'])
  },
  watch: {
    letter() {
      if (this.letter) {
        // better-scroll API
        const element = this.$refs[this.letter][0];
        this.scroll.scrollToElement(element);
      }
    }
  }
};
</script>
<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.border-topbottom::before, .border-topbottom::after {
  background-color: #ccc;
}

.border-bottom::before {
  background-color: #ccc;
}

.list {
  position: absolute;
  overflow: hidden;
  top: 1.78rem;
  left: 0;
  right: 0;
  bottom: 0;

  .title {
    background-color: #eee;
    color: #666;
    padding-left: 0.2rem;
    line-height: 0.44rem;
    font-size: 0.26;
  }

  .button-list {
    padding: 0.1rem 0.2rem 0.1rem 0.2rem;
    overflow: hidden;

    .button-wrapper {
      float: left;
      width: 33.33%;

      .button {
        margin: 0.1rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        padding: 0.1rem;
        box-sizing: border-box;
      }
    }
  }

  .item-list {
    line-height: 0.5rem;
    text-indent: 0.2rem;
  }
}
</style>